<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:meet:add']"
          >新增会议室</el-button
        >
      </el-col>
      <el-button
        size="mini"
        type="text"
        style="float: right"
        @click="myMeet"
        v-hasPermi="['system:meet:myMeet']"
        >我的预定</el-button
      >
    </el-row>

    <el-table v-loading="loading" :data="roleList">
      <el-table-column label="会议室名称" prop="name" />
      <el-table-column
        label="地点"
        prop="address"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="可容纳人数"
        prop="capacity"
        align="center"
        :show-overflow-tooltip="true"
        width="100"
      />
      <el-table-column
        label="设备情况"
        align="center"
        prop="deviceStatus"
        width="130"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.equipment_status"
            :value="scope.row.deviceStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="管理员" align="center" prop="roleSort" width="80">
        <template slot-scope="scope">
          <div>{{ getUserName(scope.row.managerId) }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        prop="roleSort"
        width="100"
        :formatter="formatter"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="onedit(scope.row, 3)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlepre(scope.row)"
            v-hasPermi="['system:meet:handlepre']"
            :disabled="scope.row.status == 1"
            >预定</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="onedit(scope.row, 2)"
            v-hasPermi="['system:meet:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:meet:del']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <addDialog ref="addDialog" @submit="getList" :userList="userList" />
    <predetermine ref="predetermine" />
    <myMeet ref="myMeet"></myMeet>
  </div>
</template>

<script>
import { roomlist, delromm, getAllUser } from "@/api/met";
import addDialog from "./addDialog.vue";
import predetermine from "./predetermine.vue";
import myMeet from "./myMeet.vue";
export default {
  name: "Role",
  dicts: ["sys_normal_disable", "equipment_status"],
  components: { addDialog, predetermine, myMeet },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      total: 0,
      // 角色表格数据
      roleList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "updateTime",
        isAsc: "desc",
      },
      userList: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    console.log(this.$route.query, "======");
    if (this.$route.query.row) {
      let data = JSON.parse(this.$route.query.row);
      this.onedit(data, 3);
    }
  },
  methods: {
    getUserName(managerId) {
      return this.userList.find((v) => v.userId == managerId)?.userName || "";
    },
    // 获取所有用户
    getAllUser() {
      getAllUser().then((res) => {
        console.log("输出所有用户", res);
        this.userList = res.data;
      });
    },
    // 我的預定
    myMeet() {
      this.$refs.myMeet.init();
    },
    formatter(row) {
      switch (row.status) {
        case 0:
          return "启用";
        case 1:
          return "禁用";
        default:
          return row.status;
      }
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      roomlist(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          console.log(response);
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addDialog.open();
    },
    //编辑
    onedit(row, type) {
      this.$refs.addDialog.open({ ...row, type: type });
    },
    // 预定
    handlepre(row) {
      console.log(row);
      this.$refs.predetermine.open(row, 1);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.id;
      this.$modal
        .confirm("是否确认删除会议室" + row.name + '"的数据项？')
        .then(() =>{
          return delromm(roleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
